package cn.cqone.crm.controller;

import cn.cqone.crm.domain.*;
import cn.cqone.crm.query.PotentialCustomerQuery;
import cn.cqone.crm.service.*;
import cn.cqone.crm.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/potCustomer")
@CrossOrigin
public class PotentialCustomerController extends BaseController<PotentialCustomer,Long,PotentialCustomerQuery> {


    @Autowired
    private PotentialCustomerService potentialCustomerService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private TenantService tenantService;

    @Autowired
    private DictoryDetailsService dictoryDetailsService;

    @Autowired
    private CustomerService customerService;

    //潜在用户下拉框
    @ResponseBody
    @RequestMapping(value = "/selectAll",method = RequestMethod.POST)
    public JSONResult selectAll(){
        try{
            List<PotentialCustomer> list = potentialCustomerService.findAll();
            return JSONResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

    //客户来源下拉框
    @ResponseBody
    @RequestMapping(value = "/selectCusSource",method = RequestMethod.POST)
    public JSONResult selectCusSource(){
        try{
            List<DictoryDetails> list = potentialCustomerService.findCusSource();
            return JSONResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

    //计划实施方式下拉框
    @ResponseBody
    @RequestMapping(value = "/selectTypePlan",method = RequestMethod.POST)
    public JSONResult selectTypePlan(){
        try{
            List<DictoryDetails> list = potentialCustomerService.findTypePlan();
            return JSONResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

    //联表新增
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public JSONResult insert(@RequestBody PotentialCustomer potentialCustomer){
        try{
            Long id = potentialCustomer.getInputUser_id().getId();
            Employee e = employeeService.findById(id);
            Tenant tenant = e.getTenant();
            potentialCustomer.setTenant_id(tenant);
            potentialCustomerService.add(potentialCustomer);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("添加失败 请仔细检查");
        }
    }

    //转为正式用户
    @RequestMapping(value = "/insertCus/{potentialCustomer_id}",method = RequestMethod.POST)
    public JSONResult insertCus(@RequestBody Customer customer,
                                @PathVariable("potentialCustomer_id")Long potentialCustomer_id){
        try{
            Long id = customer.getInputUser_id().getId();
            Employee e = employeeService.findById(id);
            Tenant tenant = e.getTenant();
            customer.setTenant_id(tenant);
            customerService.add(customer);
            potentialCustomerService.delete(potentialCustomer_id);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("添加失败 请仔细检查");
        }
    }

    //联表修改
    @RequestMapping(value = "/updateAll",method = RequestMethod.POST)
    public JSONResult updateAll(@RequestBody PotentialCustomer potentialCustomer){
        try{
            Long id = potentialCustomer.getInputUser_id().getId();
            Employee e = employeeService.findById(id);
            Tenant tenant = e.getTenant();
            potentialCustomer.setTenant_id(tenant);
            potentialCustomerService.update(potentialCustomer);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("修改失败 请仔细检查");
        }
    }

}
